<!--
Mark O'Meara
2011/2012
-->

<html>
<head>
<title>Mark's FB app!</title>

<script type="text/javascript" language="javascript">

var appid = 193793277381130;
//var pageurl = "http://localhost/";
//var pageurl_old = "http://macneill.scss.tcd.ie/~omearam1/fbapp.html";
var pageurl = "http://www.mark-omeara.ie/fbapp/fb_friendsapp.html";

/*Function for display user's name*/
function dispName(data){
	var total = document.getElementById('total');
	//Print "Greetings, " followed by the user's Facebook name	
//	var greetingText = document.createTextNode('Greetings, ' + user.name + '.');
 //  	userName.appendChild(greetingText);
	
	var received = "";
	var i = 0;

	/*Sort in alphabetical order*/
	data = sortAlpha(data);
	

	
	//Print out list of friends
	for(i = 0; i < data.data.length; i++){
	
		var existing = document.getElementById('list').innerHTML;
		document.getElementById('list').innerHTML = existing + "<tr><td>" + data.data[i].name + " " + data.data[i].id + "</td></tr>"; 
	}
	var sum = "Total Friends: " + i;
	document.getElementById('total').innerHTML = "Detected" + "<b> " + data.data.length + " </b>" + "friends.";
	//document.getElementById('total').innerHTML = "Length: " + data.data.length;
}


/*Function for initialising everything*/
function initFunc(){
	var notoken = "true";
	//Checks that there's a hash parameter
	if(location.hash){
		//Parses the parameters
		var hashes = location.hash.split('#')[1];
		var spl = hashes.split('&')[0].split('=');
		//If the hash parameter is the access_token
		if(spl[0] == "access_token"){
			/*spl now holds 'access_token' in first element, and the 
			access token value in the second element*/
			var tokenval = spl[1];
			notoken = "false";
			/*Gets the user info and sends it to the dispName function*/
			var script = document.createElement('script');
			script.src= "https://graph.facebook.com/me/friends?access_token=" + tokenval + "&callback=dispName";
			document.body.appendChild(script);
		}
	}
	//If we don't have the access token, get it!
	if(notoken == "true"){
		window.location = "https://www.facebook.com/dialog/oauth?client_id=" + appid + "&redirect_uri=" + pageurl + "&response_type=token";
	}
}

function sortAlpha(data){
	var lim = data.data.length;
	var j;
	var k;
	for(k = data.data.length; k >= 0; k--){
		for(j = 0; j < lim - 1; j++){
			var t = 0;
			for(t = 0; t < data.data[j].name.length && t < data.data[j+1].name.length && data.data[j].name[t] == data.data[j+1].name[t]; t++){
			}
							
			if(t < data.data[j].name.length && t < data.data[j+1].name.length && data.data[j].name[t] > data.data[j+1].name[t]){
		//	if(data.data[j].name[0] > data.data[j+1].name[0]){
				var temp = data.data[j];
				data.data[j] = data.data[j+1];
				data.data[j+1] = temp;
			}
		}
	}
	return data;


}

</script>
</head>
<body>
<script type="text/javascript">
//Call the JavaScript function that does everything!
initFunc();
</script>
<!--This defines the paragraph where the greeting will be printed -->
<p id="total"></p>
<table id="list"></table>
</body>
</html>
